#!/usr/bin/perl
#
# Write the "short" report that lists Linux users
# by place of work, country and how long they have used Linux
#
# The country table also includes registered friends and machines
#

require "reportsub.pl";

$personfile = shift;
$friendfile = shift;
$machinefile = shift;
$personfile = "reports/persons.count" if !$personfile;
$machinefile = "reports/machines.count" if !$machinefile;
$friendfile = "reports/friends.count" if !$friendfile;

#===================== DATA FETCH =======================

readfile($personfile, \%persons, \%perssums);
readfile($friendfile, \%friends, \%friendsums);
readfile($machinefile, \%machines, \%machinesums);

open(POP, "data/population");
while (<POP>) {
    chop;
    next if /^#/;
    ($country, $long, $pop) = split(/\t/);
    $longname{$country} = $long;
    $population{$country} = $pop;
}

# Calculate some other numbers
for $country (keys(%population)) {
    if ($population{$country} > 0) {
	$userdensity{$country} =
	    $persons{country}{"\L$country"} / $population{$country};
    } elsif ($persons{country}{"\L$country"}) {
	print STDERR "$country: Users but no population\n";
    }
}

$personcount = $perssums{country}{total} + $perssums{country}{empty};
$friendcount = $friendsums{country}{total} + $friendsums{country}{empty};
$machinecount = $machinesums{cpu}{total} + $machinesums{cpu}{empty};
$date = localtime;

# =============== Introductory remarks ==========================
$mincount = printwithcommas($personcount*20);
$maxcount = printwithcommas($personcount*500);
print <<EoF;

This is the Linux Counter summary as of $date

There are $personcount persons registerd.
$friendcount users have been registered by friends.
There are $machinecount machines registered.

I guesstimate that between 0.2% and 5% of all Linux users have
registered with the Linux Counter.
So the total number of Linux users is probably between
$mincount and $maxcount people.

EoF


# =============== List countries by density =====================

print "WHERE LINUX USERS LIVE\n";
print "The table is sorted by number of Linux users divided by population\n\n";

printf "%3s %2s %-28s %5s %4s %4s %5s %6s\n",
    "No", "", "Country", "Pers", "Fri", "Mach", "P/Mpop", "Mpop";
print "=" x 70, "\n";
$rank = 1;
for $country (sort {$userdensity{$b} <=> $userdensity{$a}}
	      keys(%population)) {
    next if (!$persons{country}{"\L$country"});
    printf "%3d %2s %-28.28s %5d %4d %4d %5.1f %6.1f\n",
    $rank,
    $country, $longname{$country}, $persons{country}{"\L$country"},
    $friends{country}{"\L$country"}, $machines{country}{"\L$country"},
    $userdensity{$country}, $population{$country};
    ++ $rank;
}
print "\n\n";

#==================== Table of users and places ==================
print "WHERE PEOPLE USE LINUX\n";
printf "%-10s %5s %5s\n",
    "Place", "Users", "Percent";
print "=" x 30, "\n";
for $usage (keys(%{$persons{usage}})) {
    printf "%-10s %5d %6.2f%%\n",
    $usage, $persons{usage}{$usage},
    100 * $persons{usage}{$usage} / $perssums{usage}{total};
}
print "=" x 30, "\n";
printf "%-10s %5d %6.2f%%\n",
    "TOTAL", $perssums{usage}{total}, 100;

print <<EoF;

NOTE: The total is the $perssums{usage}{total} users who answered this question.
Some gave more than one answer, so the number of answers is $perssums{usage}{values}

EoF
